package roborally;

import java.util.Random;

import Exceptions.ElementPlacementError;
import Exceptions.IllegalCoordinateException;
import Exceptions.TerminationError;
import be.kuleuven.cs.som.annotate.Raw;

/**
 * A class representing surpriseboxes with a weight.
 * 
 * @author Jef Keijers/Bachelor of Science: Informatics
 * @version 1.0
 */
public class SurpriseBox extends Item {
	
	/**
	 * @param 	weight
	 * @effect	
	 * 			|super(weight)
	 */
	public SurpriseBox(int weight) {
		super(weight);
	}

	
	/**
	 * @throws 	IllegalCoordinateException 
	 * 			|if (!(isProperPosition(position)))
	 */
	@Override @Raw
	public void handleShot() throws IllegalCoordinateException {
		long surpriseBoxX=this.getPosition().getX();
		long surpriseBoxY=this.getPosition().getY();
		this.handleShotOnPosition(new Position(surpriseBoxX-1,surpriseBoxY-1,this.getBoard())); // linksboven
		this.handleShotOnPosition(new Position(surpriseBoxX,surpriseBoxY-1,this.getBoard())); // boven
		this.handleShotOnPosition(new Position(surpriseBoxX+1,surpriseBoxY-1,this.getBoard())); // rechtsboven
		this.handleShotOnPosition(new Position(surpriseBoxX-1,surpriseBoxY,this.getBoard())); // links
		this.handleShotOnPosition(new Position(surpriseBoxX+1,surpriseBoxY,this.getBoard())); // rechts
		this.handleShotOnPosition(new Position(surpriseBoxX-1,surpriseBoxY+1,this.getBoard())); //linksonder
		this.handleShotOnPosition(new Position(surpriseBoxX,surpriseBoxY+1,this.getBoard())); // onder
		this.handleShotOnPosition(new Position(surpriseBoxX+1,surpriseBoxY+1,this.getBoard()));	 //rechtsonder
		this.handleShotOnPosition(new Position(surpriseBoxX,surpriseBoxY,this.getBoard()));  //plaats zelf
			
	}
	
	/**
	 * @param 	position
	 * @post 	| this.getBoard().getHashMap().get(position).isEmpty() == true
	 */
	@Raw
	public void handleShotOnPosition(Position position) {
		Board boardBackup=this.getBoard();		
		if (boardBackup.getPositionMap().containsKey(position)) {		
			for (Element element:boardBackup.getPositionMap().get(position)) {
				element.terminate();
			}		
			boardBackup.getPositionMap().get(position).clear();
			boardBackup=null; 
		}
	}

	/**
	 * @param robot
	 * @post| if (random.nextInt(5) == 0) then robot.isTerminated == true
	 * 		| if (random.nextInt(5) == 1) then new.robot.getPosition() == new Position(random.nextInt((int) robot.getBoard().getWidth()),random.nextInt((int) robot.getBoard().getHeight()),robot.getBoard())
	 * 		| if (random.nextInt(5) == 2) then new.robot.getPossessions().contains(new Battery(1000,1000)) == true
	 * 		| if (random.nextInt(5) == 3) then new.robot.getPossessions().contains(new RepairKit(1000, 1000)) == true
	 * 		| if (random.nextInt(5) == 0) then new.robot.getPossessions().contains(new SurpriseBox(1000)) == true
	 * 		| new.robot.getPossessions().contains(this) == false
	 * 		| new.isTerminated == true
	 */
	@Override @Raw
	public void handleUse(Robot robot) throws TerminationError, IllegalCoordinateException, ElementPlacementError {
		Random random=new Random();
		
		switch (random.nextInt(5)) {
		
		case 0: 
				robot.getBoard().terminateFromBoard(robot, this.getPosition());
				System.out.println("Robot is terminated!");
				break;			
		case 1:
				int xCoordinate=random.nextInt((int) robot.getBoard().getWidth());
				int yCoordinate=random.nextInt((int) robot.getBoard().getHeight());
				Board backupBoard=robot.getBoard();
				robot.getBoard().terminateFromBoard(robot,robot.getPosition());
				robot.putRobot(backupBoard, xCoordinate, yCoordinate);
				System.out.println("Robot placed to position " +xCoordinate+ ", " +yCoordinate);
				break;
		case 2:
				//contains battery
				robot.getPossessionList().add(new Battery(1000,1000));
				System.out.println("Surprise! Surprise! You've awarded a battery!");
				break;
		case 3:
				//contains repairkit
				robot.getPossessionList().add(new RepairKit(1000, 1000));
				System.out.println("Surprise! Surprise! You've awarded a repairkit");
				break;
		case 4:
			// contains surprisebox	
				robot.getPossessionList().add(new SurpriseBox(1000));
				System.out.println("Surprise! Surprise! You've awarded a new Surprisebox!");
				break;
		}
		robot.terminateFromPossessions(this);
		this.terminate();
	}

}
